#!usr/bin/env python  
# -*- coding:utf-8 -*-
""" 
@author:robot
@file: reverse_link.py 
@version:
@time: 2024/01/07 
"""


class ListNode:
    def __init__(self, v):
        self.value = v
        self.next = None


t0 = ListNode(1)
t1 = ListNode(2)
t2 = ListNode(2)
t3 = ListNode(1)
t0.next = t1
t1.next = t2
t2.next = t3


def is_palindrome(head):
    if head is None or head.next is None: return True
    fast = slow = head
    while fast.next is not None and fast.next.next is not None:
        slow = slow.next
        fast = fast.next.next
    # 反转剩余的列表
    start = reverse_link(slow.next)

    # 通过反转列表判断它们的值是否相等
    # head -> 1->2->2->1
    # 2<-1 <- start
    while start is not None:
        if head.value != start.value:
            return False
        head = head.next
        start = start.next
    return True


def reverse_link(head):
    pre = None
    while head is not None:
        current = head.next
        head.next = pre
        pre = head
        head = current
    return pre


if __name__ == '__main__':
    res = is_palindrome(t0)
    print(res)
